package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.mybeans.form.FormBeanException;
import org.mybeans.form.FormBeanFactory;

import bean.Customer;
import bean.Model;
import bean.User;
import bean.UserType;
import dao.CustomerDAO;
import dao.UserDAO;
import formBean.CustomerAc;

public class CreateCustomerAction extends Action {
	private FormBeanFactory<CustomerAc> formBeanFactory = FormBeanFactory.getInstance(CustomerAc.class);
	private CustomerDAO customerDAO;
	private UserDAO userDAO;
	
	public CreateCustomerAction(Model model) {
		this.customerDAO = model.getCustomerDAO();
		this.userDAO = model.getUserDAO();
	}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
	
	@Override
	public String getName() {
		return "createCustomer.do";
	}

	@Override
	public String perform(HttpServletRequest request) {
		List<String> errors = new ArrayList<String>();
		request.setAttribute("errors", errors);
		
		try {
			CustomerAc form = formBeanFactory.create(request);
			request.setAttribute("form", form);
			
			if (!form.isPresent()) {
				return "customerAc.jsp";
			}
			
			errors.addAll(form.getValidationErrors());
			
			if (errors.size() > 0) {
				return "customerAc.jsp";
			}
			
//			Employee e = employeeDAO.lookUpByUsername(form.getUserName());
			User user = userDAO.lookUpByUsername(form.getUserName());
			
			if (user != null) {
				errors.add("The username already exists.");
				return "customerAc.jsp";
			}
			
			Customer c = new Customer();
			c.setFirstname(form.getFirstName());
			c.setLastname(form.getLastName());
			c.setUsername(form.getUserName());
			c.setPassword(form.getPassword());
			c.setCash(form.getCash());
			c.setAddrLine1(form.getAddress1());
			c.setAddrLine2(form.getAddress2());
			c.setCity(form.getCity());
			c.setState(form.getState());
			c.setZip(form.getZipCode());
			c.setPhoneNum(form.getPhoneNo());
			
			errors.addAll(customerDAO.createCustomer(c));
			
			if (errors.size() > 0) return "customerAc.jsp";
			
			c = customerDAO.getCustomerByUsername(form.getUserName());
			User newUser = new User();
			newUser.setCustomer(true);
			newUser.setUserId(c.getId());
			newUser.setUsername(form.getUserName());
			errors.addAll(userDAO.create(newUser));
			
			if (errors.size() > 0) return "customerAc.jsp";
			
			request.setAttribute("message", "You have created the customer " + c.getUsername() + ".");
			return "employeeConsole.jsp";			
		} catch (FormBeanException e) {
			e.printStackTrace();
			errors.add(e.getMessage());
			return "customerAc.jsp";
		}
	}

	@Override
	public UserType getType() {
		return UserType.Employee;
	}
}
